<?php


namespace app\common\controller;


use think\Config;
use think\Db;
use think\Exception;
use think\Session;

class Together extends Common
{

    //导出报名名单
    public function export(){

        //性别
        $sex = Db::name("config_attribute")->where(['key' => "sex"])->value("value");
        $sex = explode("|", $sex);
        foreach ($sex as $k => $v) {
            $sex[$k + 1] = $v;
        }
        unset($sex[0]);
        //获取订单列表
        $where['a.content_id'] = $_POST['id'];
        $lists = Db::name("admin_content_user")->alias("a")
            ->join("admin_registration b", "a.fans_id = b.id")
            ->where($where)->field("a.id,b.name,b.sex,b.mobile,b.housing_id")->select();

        foreach ($lists as $k=>$v){
            //获取小区信息
            $housing = Db::name("admin_community_housing")->alias("a")
                ->join("admin_community_element b","a.element_id = b.id")
                ->join("admin_community_building c","a.building_id = c.id")
                ->join("admin_community d","a.community_id = d.id")
                ->where(['a.id'=>$v['housing_id']])
                ->field("a.roomNumber,b.elementName,c.buildingName,d.propertyName")->find();
            $lists[$k]['housing'] = $housing['propertyName'].$housing['buildingName'].$housing['elementName'].$housing['roomNumber'];
            $lists[$k]['sex'] = $sex[$v['sex']];
        }

        require ROOT_PATH."extend/PHPExce/PHPExcel.php";//引入PHPExcel加载文件
        $objPHPExcel=new \PHPExcel();
        $objPHPExcel->getProperties()->setCreator('http://www.jb51.net')
            ->setLastModifiedBy('http://www.jb51.net')
            ->setTitle('Office 2007 XLSX Document')
            ->setSubject('Office 2007 XLSX Document')
            ->setDescription('Document for Office 2007 XLSX, generated using PHP classes.')
            ->setKeywords('office 2007 openxml php')
            ->setCategory('Result file');
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1','姓名')
            ->setCellValue('B1','性别')
            ->setCellValue('C1','联系方式')
            ->setCellValue('D1','所在小区');

        #设置单元格宽高
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);#设置单元格宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(60);

        $i=2;
        foreach ($lists as $k=>$v){
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A'.$i,$v['name'])
                ->setCellValue('B'.$i,$v['sex'])
                ->setCellValue('C'.$i,$v['mobile'])
                ->setCellValue('D'.$i,$v['housing']);
            $i++;
        }

        $objPHPExcel->getActiveSheet()->setTitle('报名名单');
        $objPHPExcel->setActiveSheetIndex(0);
        //$filename=urlencode('订单数据').'_'.date('Y-m-dHis');
        $filename='报名名单'.'_'.date('Y-m-dHis');


        //*生成xlsx文件
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
        header('Cache-Control: max-age=0');
        $objWriter=\PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');


        /*
        *生成xls文件
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        */
        $objWriter->save('php://output');
        $xlsData = ob_get_contents();
        ob_end_clean();
        $return = ['filename' => $filename, 'file' => "data:application/vnd.ms-excel;base64," . base64_encode($xlsData)];
        return $return;
        exit;

    }

    //审核快捷操作
    public function clickStatus()
    {
        $nolog = 0;
        try {
            $common = new Common();
            $data['status'] = $_POST['status'];
            $table = empty($_POST['table'])?"admin_registration":$_POST['table'];
            Db::name($table)->where(['id' => $_POST['id']])->update($data);
            $log['controller'] = "Together";
            $log['methods'] = "clickStatus";

            $registration = Db::name($table)->where(['id' => $_POST['id']])->find();
            /*if($table=="admin_owner" || $table=="admin_affiche" || $table=="admin_news"){
                $registration = Db::name($table)->where(['id' => $_POST['id']])->field("id")->find();
            }else{
                $registration = Db::name($table)->where(['id' => $_POST['id']])->field("id,name")->find();
            }*/

            $status = ($_POST['status']==1)?"未审核":"已审核";
            if($table=="admin_registration"){
                $log['remark'] = "账号【" . Session::get("user_name") . "】将志愿者【".$registration['name']."】id【".$registration['id']."】的状态更新为【".$status."】";
            }elseif ($table=="admin_slide"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                $log['remark'] = "账号【" . Session::get("user_name") . "】将幻灯片【".$registration['name']."】id【".$registration['id']."】的状态更新为【".$status."】";
            }elseif ($table=="admin_news_classify"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                $log['remark'] = "账号【" . Session::get("user_name") . "】将资讯分类【".$registration['name']."】id【".$registration['id']."】的状态更新为【".$status."】";
            }elseif ($table=="admin_banner"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                $log['remark'] = "账号【" . Session::get("user_name") . "】将幻灯片【".$registration['name']."】id【".$registration['id']."】的状态更新为【".$status."】";
            }elseif ($table=="admin_column"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                $log['remark'] = "账号【" . Session::get("user_name") . "】将平台导航【".$registration['name']."】id【".$registration['id']."】的状态更新为【".$status."】";
            }elseif ($table=="admin_owner"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                $log['remark'] = "账号【" . Session::get("user_name") . "】将业主id【".$registration['id']."】的状态更新为【".$status."】";
            }elseif ($table=="admin_affiche"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                $log['remark'] = "账号【" . Session::get("user_name") . "】将公告id【".$registration['id']."】的状态更新为【".$status."】";
            }elseif ($table=="admin_news"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                $log['remark'] = "账号【" . Session::get("user_name") . "】将新闻id【".$registration['id']."】的状态更新为【".$status."】";
            }elseif ($table=="admin_device"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                $log['remark'] = "账号【" . Session::get("user_name") . "】将设备【".$registration['name']."】id【".$registration['id']."】的状态更新为【".$status."】";
            }elseif ($table=="admin_colony") {
                $status = ($_POST['status'] == 1) ? "启用" : "禁用";
                $log['remark'] = "账号【" . Session::get("user_name") . "】将社区【" . $registration['name'] . "】id【" . $registration['id'] . "】的状态更新为【" . $status . "】";
            }elseif ($table=="admin_stores_manage"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                $log['remark'] = "账号【" . Session::get("user_name") . "】将商户账号【".$registration['name']."】id【".$registration['id']."】的状态更新为【".$status."】";
            }elseif ($table=="admin_manager"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                $log['remark'] = "账号【" . Session::get("user_name") . "】将小区管理员【".$registration['name']."】id【".$registration['id']."】的状态更新为【".$status."】";
            }elseif ($table=="admin_gridmember"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                $log['remark'] = "账号【" . Session::get("user_name") . "】将社区网格员【".$registration['name']."】id【".$registration['id']."】的状态更新为【".$status."】";
            }elseif ($table=="admin_nav"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                 //获取小区名称
                $propertyName = Db::name("admin_colony")->where(['id'=>$registration['colony_id']])->value("name");
                $log['remark'] = "账号【" . Session::get("user_name") . "】将社区【".$propertyName."】的导航【".$registration['name']."】id【".$registration['id']."】的状态更新为【".$status."】";

            }elseif ($table=="admin_article_classify"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                //获取小区名称
                $propertyName = Db::name("admin_colony")->where(['id'=>$registration['colony_id']])->value("name");
                $log['remark'] = "账号【" . Session::get("user_name") . "】将社区【".$propertyName."】的栏目【".$registration['name']."】id【".$registration['id']."】的状态更新为【".$status."】";

            }elseif ($table=="admin_article_tag"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                //获取小区名称
                $propertyName = Db::name("admin_colony")->where(['id'=>$registration['colony_id']])->value("name");
                $log['remark'] = "账号【" . Session::get("user_name") . "】将社区【".$propertyName."】的标签【".$registration['name']."】id【".$registration['id']."】的状态更新为【".$status."】";

            }elseif ($table=="admin_content"){
                $status = ($_POST['status']==1)?"启用":"禁用";

                if(!empty($_POST['content'])){
                    $log['remark'] = "账号【" . Session::get("user_name") . "】将一键通【".$registration['title']."】id【".$registration['id']."】的状态更新为【".$status."】";
                }else{
                    $log['remark'] = "账号【" . Session::get("user_name") . "】将社区发布的内容【".$registration['title']."】id【".$registration['id']."】的状态更新为【".$status."】";
                }
            }elseif ($table=="admin_colony_manager"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                $log['remark'] = "账号【" . Session::get("user_name") . "】将社区管理员的账号【".$registration['name']."】id【".$registration['id']."】的状态更新为【".$status."】";

            }elseif ($table=="admin_exchange"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                $log['remark'] = "账号【" . Session::get("user_name") . "】将兑换商品【".$registration['title']."】id【".$registration['id']."】的状态更新为【".$status."】";
            }elseif ($table=="admin_navigation"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                $log['remark'] = "账号【" . Session::get("user_name") . "】将导航【".$registration['text']."】的状态更新为【".$status."】";

            }elseif ($table=="admin_cartoon"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                $log['remark'] = "账号【" . Session::get("user_name") . "】将平台动画【".$registration['title']."】的状态更新为【".$status."】";

            }elseif ($table=="admin_ptt"){
                $status = ($_POST['status']==1)?"启用":"禁用";
                $log['remark'] = "账号【" . Session::get("user_name") . "】将一键通的分类【".$registration['name']."】的状态更新为【".$status."】";

            }else{
                //$log['remark'] = "账号【" . Session::get("user_name") . "】将党员【".$registration['name']."】id【".$registration['id']."】的状态更新为【".$status."】";
               $nolog = 1;
            }
            if($nolog != 1){
                $common->addLog($log);
            }
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //获取志愿者列表
    public function userList()
    {

        if (empty($_POST['type'])) {
            if (empty($_POST['keyword'])) {
                $black['code'] = 0;
                $black['data'] = [];
                $black['msg'] = "暂无数据";

            } else {
                //$sex = Config::get("sex");
                $sex = Db::name("config_attribute")->where(['key' => "sex"])->value("value");
                $sex = explode("|", $sex);
                foreach ($sex as $k => $v) {
                    $sex[$k + 1] = $v;
                }
                unset($sex[0]);

                //根据id取出社区id
                $colony_id = Db::name("admin_content")->alias("a")->join("admin_article_classify b","a.classify_id = b.id")->where(['a.id'=>$_POST['id']])->value("b.colony_id");
                $where = "a.colony_id = {$colony_id}";

                $keyword = $_POST['keyword'];
//                $whereOr['a.name'] = ['like', "%" . $keyword . "%"];
//                $whereOr['a.id'] = intval($keyword);
//                $whereOr['a.mobile'] = ['like', "%" . $keyword . "%"];
//                $whereOr['b.propertyName'] = ['like', "%" . $keyword . "%"];
                $where .=" and (a.id = ".intval($keyword)." or a.name like '%{$keyword}%' or a.mobile like '%{$keyword}%' or b.propertyName like '%{$keyword}%')";

                $data = Db::name("admin_registration")->alias("a")
                    ->join("admin_community b","a.community_id = b.id")
                    ->where($where)
                    ->field("a.*,b.propertyName")->select();


                foreach ($data as $k => $v) {
                    $data[$k]['sex'] = $sex[$v['sex']];
//                    $data[$k]['idCard'] = substr_replace($v['idCard'], str_repeat('*', 6), 6, 8);
//                    $data[$k]['mobile'] = substr($v['mobile'], 0, 3) . '****' . substr($v['mobile'], 7, strlen($v['mobile']));;
                }
                $black['code'] = 0;
                $black['data'] = $data;
            }
            return $black;
        } else {

            $id = $_POST['id'];
            $this->assign("id", $id);
            return view("userList");
        }
    }


    //根据社区区获取对应栏目
    public function findNav(){

        try{

            $community_id = $_POST['community_id'];
            $where['colony_id'] = $community_id;
            $where['status'] = 1;
            $nav = Db::name("admin_nav")->field("id,name")->where($where)->select();

            $black['code'] = 0;
            $black['msg'] = "添加成功";
            $black['data'] = $nav;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;

    }


    //公共条件
    public function publicWhere(){
        $where = [];
        $auto = Session::get("auto");
        if(!empty($auto)){
            switch ($auto){
                case "1":
                    //查询所代理的区域
                    $cityWhere['agent_id'] = Session::get("id");
                    $agent_city = Db::name("admin_agent_city")->whereOr($cityWhere)->field("area")->select();
                    $agent_city = array_column($agent_city,"area");
                    $agent_city = empty($agent_city)?-1:$agent_city;
                    $where['b.cityRegionId']= ['in',$agent_city];
                    break;
                case "2":
                    //查询所管理的小区
                    $cityWhere['manager_id'] = Session::get("id");
                    $community_id = Db::name("admin_manager_community")->whereOr($cityWhere)->field("community_id")->select();
                    $community_id = array_column($community_id, "community_id");
                    $community_id = empty($community_id)?-1:$community_id;
                    $where['b.id']= ['in',$community_id];
                    break;
                case "3":
                    //查询所管理的小区
                    $cityWhere['gridmember_id'] = Session::get("id");
                    $community_id = Db::name("admin_gridmember_community")->whereOr($cityWhere)->field("community_id")->select();
                    $community_id = array_column($community_id, "community_id");
                    $community_id = implode($community_id, ",");
                    $community_id = empty($community_id)?-1:$community_id;
                    $where['b.id']= ['in',$community_id];
                    break;
                default:
            }
        }
        return $where;
    }

    //查询社区
    public function findColony(){
        $where = [];

        $thisWechat = Session::get("thisWechat");
        $where['uniacid'] = $thisWechat['id'];

        //查询社区
        $community = Db::name("community_colony")->field("id,name")->where($where)->select();
        return $community;
    }

    //获取社区下面的小区
    public function findColonyList($colony_id=0){
        if($_POST){
            $where['a.colony_id'] = $_POST['id'];
            $community = Db::name("community_colony_community")->alias("a")->join("admin_community b","a.community_id = b.id")
                ->where($where)->field("b.id,b.propertyName")->select();
            if(!empty($_POST['type'])){
                $classify = Db::name("admin_article_classify")->field("id,name")->where(['colony_id'=>$_POST['id'],'type'=>2])->select();
                $black['classify'] = $classify;
            }
            $black['code'] = 0;
            $black['data'] = $community;
            return $black;
        }else{
            $where['a.colony_id'] = $colony_id;
            $community = Db::name("community_colony_community")->alias("a")->join("admin_community b","a.community_id = b.id")
                ->where($where)->field("b.id,b.propertyName")->select();
            return $community;
        }
    }

    //获取社区下面的小区
    public function findClassify(){
        if($_POST){
            $classify = Db::name("admin_article_classify")->field("id,name")->where(['colony_id'=>$_POST['id'],'type'=>10])->select();
            $black['classify'] = $classify;
            $black['code'] = 0;
            return $black;
        }
    }


    //获取楼栋地址信息
    public function getRegion($community_id=0)
    {
        if($_POST){
            $where['community_id'] = $_POST['id'];
            //查询楼栋信息
            $building = Db::name("admin_community_building")->where($where)->field("id,buildingName")->select();
            $black['code'] = 0;
            $black['data'] = $building;
            return $black;
        }else{
            $where['community_id'] = $community_id;
            //查询楼栋信息
            $building = Db::name("admin_community_building")->where($where)->field("id,buildingName")->select();
            return $building;
        }
    }

    //获取单元信息
    public function getElement($building_id=0)
    {
        if($_POST){
            $where['building_id'] = $_POST['id'];
            //查询单元信息
            $element = Db::name("admin_community_element")->where($where)->field("id,elementName,floor,roomLayout")->select();
            $black['code'] = 0;
            $black['data'] = $element;
            return $black;
        }else{
            $where['building_id'] = $building_id;
            //查询单元信息
            $element = Db::name("admin_community_element")->where($where)->field("id,elementName,floor,roomLayout")->select();
            return $element;
        }
    }


    //获取单元的楼层数&户数
    public function getFloor()
    {
        $id = $_POST['id'];
        $where['id'] = $id;
        //查询单元信息
        $data = Db::name("admin_community_element")->where($where)->field("floor,roomLayout")->find();

        $floor = [];
        for ($i = 1; $i <= $data['floor']; $i++) {
            $floor[] = intval($i);
        }



        $room = [];
        for ($j = 1; $j <= $data['roomLayout']; $j++) {
            $room[] = sprintf("%02d", $j);
        }

        $black['code'] = 0;
        $black['floor'] = $floor;
        $black['room'] = $room;
        return $black;
    }


    //查询小区
    public function findCommunity(){
        $where = [];
        $auto = Session::get("auto");
        if(!empty($auto)){
            switch ($auto){
                case "1":
                    //查询所代理的区域
                    $cityWhere['agent_id'] = Session::get("id");
                    $agent_city = Db::name("admin_agent_city")->whereOr($cityWhere)->field("area")->select();
                    $agent_city = array_column($agent_city,"area");
                    $agent_city = empty($agent_city)?-1:$agent_city;
                    $where['cityRegionId']= ['in',$agent_city];
                    break;
                case "2":
                    //查询所管理的小区
                    $cityWhere['manager_id'] = Session::get("id");
                    $community_id = Db::name("admin_manager_community")->whereOr($cityWhere)->field("community_id")->select();
                    $community_id = array_column($community_id, "community_id");
                    $community_id = empty($community_id)?-1:$community_id;
                    $where['id']= ['in',$community_id];
                    break;
                case "3":
                    //查询所管理的小区
                    $cityWhere['gridmember_id'] = Session::get("id");
                    $community_id = Db::name("admin_gridmember_community")->whereOr($cityWhere)->field("community_id")->select();
                    $community_id = array_column($community_id, "community_id");
                    $community_id = implode($community_id, ",");
                    $community_id = empty($community_id)?-1:$community_id;
                    $where['id']= ['in',$community_id];
                    break;
                default:
            }
        }
        //查询所代理的小区
        $community = Db::name("admin_community")->field("id,propertyName")->where($where)->select();
        return $community;
    }


    //查询所代理的区域
    public function ColonyCity($type = 0){
        $auto = Session::get("auto");
        $agent_city = '';
        if(!empty($auto)){
            switch ($auto){
                case "1":
                    //查询所代理的区域
                    $cityWhere['agent_id'] = Session::get("id");
                    $agent_city = Db::name("admin_agent_city")->whereOr($cityWhere)->field("area")->select();
                    $agent_city = array_column($agent_city,"area");
                    if($type==0){
                        $agent_city = implode($agent_city,",");
                        $agent_city = empty($agent_city)?-1:$agent_city;
                    }
                    break;
            }
        }
        return $agent_city;
    }


}